package com.zhongjh.app.phone.shoppingcart

import android.view.View
import android.widget.TextView
import com.zhongjh.mvvmibatis.entity.State
import kotlinx.coroutines.flow.MutableStateFlow

/**
 * 简单处理购物车圆形数据
 * @author zhongjh
 * @date 2022/7/13
 */
class ShoppingCartViewManager(var tvShoppingCartCount: TextView, amount: Int) {

    var amount: Int = 0
        set(value) {
            field = value
            amountChange()
        }

    init {
        if (amount > 0) {
            this.amount = amount
            tvShoppingCartCount.visibility = View.GONE
        }
    }

    private fun amountChange() {
        if (amount > 0) {
            tvShoppingCartCount.visibility = View.VISIBLE
            tvShoppingCartCount.text = amount.toString()
        } else {
            tvShoppingCartCount.visibility = View.GONE
        }
    }
}

/**
 * 用于接收购物车数量的最新数据
 */
val shoppingCartCountFlow = MutableStateFlow<State<Int>>(State.Empty())